package com.tbit.main.service.impl;

import cn.hutool.core.date.DateUtil;
import com.tbit.common.constant.HandleResultConstant;
import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.constant.UserMsgConstant;
import com.tbit.main.dao.log.ViolationLogDao;
import com.tbit.main.pojo.*;
import com.tbit.main.service.*;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 运维举报
 *
 * @author Leon
 * 2020年3月11日 下午8:22:36
 */
@Service("violationLogService")
public class ViolationLogServiceImpl implements ViolationLogService {
    @Autowired
    private ViolationLogDao violationLogDao;
    @Autowired
    private UserMsgService userMsgService;
    @Autowired
    private UserBlackListService userBlackListService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private BlackListLogService blackListLogService;

    @Override
    public void insert(ViolationLog violationLog) {
        violationLogDao.insert(violationLog);
    }

    @Override
    public void handle(ViolationLog violationLog) {
        violationLogDao.handle(violationLog);
    }

    @Override
    public ViolationLog getById(Integer violationLogId) {
        return violationLogDao.getById(violationLogId);
    }

    @Override
    public List<ViolationLog> query(Integer accountId, String userPhone, Integer violationState, String startTime, String endTime, List<Integer> regionId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put("userPhone", userPhone);
        params.put("violationState", violationState);
        params.put(DaoParamConstant.startTime, startTime);
        params.put(DaoParamConstant.endTime, endTime);
        params.put("regionId", StringUtil.getListString(regionId));

        return violationLogDao.query(params);
    }

    @Override
    public void handleNew(AccountUser accountUser, Integer violationLogId, Integer handleResult, String handleDesc,
                          Integer type, String blockTime, String photosUrl) {
        ViolationLog violationLog = new ViolationLog();
        violationLog.setViolationLogId(violationLogId);
        violationLog.setViolationState(1);
        violationLog.setHandleUserId(accountUser.getAccountUserId());
        violationLog.setHandleTime(DateTimeUtil.getNowTime());
        violationLog.setHandleResult(handleResult == 1 ? HandleResultConstant.PASSED : HandleResultConstant.REFUSED);
        violationLog.setHandleUserName(accountUser.getName());
        violationLog.setHandleDesc(handleDesc);
        ViolationLog violationLogDb = this.getById(violationLogId);
        if (Objects.equals(type, 1)) {
            UserMsg userMsg = new UserMsg();
            userMsg.setMsgType(UserMsgConstant.UserMsg_PERSON);
            userMsg.setUserId(violationLogDb.getUserId());
            userMsg.setCreateTime(DateTimeUtil.getNowTime());
            userMsg.setMsgState(0);
            userMsg.setTitle("【温馨提示】");
            userMsg.setContent(handleDesc);
            userMsg.setRemark("运维端发送");
            userMsgService.insert(userMsg);
            violationLog.setHandleDesc("回复用户，回复内容：" + handleDesc);
            this.handle(violationLog);
        } else if (Objects.equals(type, 2)) {
            Integer brandId = accountService.getByAccount(violationLogDb.getAccountId());
            UserBlackList userBlackList = new UserBlackList();
            userBlackList.setUserId(violationLogDb.getUserId());
            userBlackList.setAccountId(brandId);
            userBlackList.setPhone(violationLogDb.getUserPhone());
            userBlackList.setAddTime(DateTimeUtil.getNowTime());
            userBlackList.setExpireTime(blockTime);
            userBlackList.setRemark("运维举报拉黑，运维端处理");
            userBlackList.setPhotosUrl(photosUrl);
            UserBlackList userBlackListDb = userBlackListService.getByUserId(violationLogDb.getUserId());
            if (userBlackListDb != null) {
                userBlackListDb.setExpireTime(blockTime);
                userBlackListDb.setRemark("运维举报拉黑，运维端处理");
                userBlackListDb.setPhotosUrl(photosUrl);
                userBlackListService.update(userBlackListDb);
            } else {
                userBlackListService.insert(userBlackList);
            }
            BlackListLog blackListLog = new BlackListLog();
            blackListLog.setUserId(violationLogDb.getUserId())
                    .setAccountId(brandId)
                    .setUserPhone(violationLogDb.getUserPhone())
                    .setAddTime(DateTimeUtil.getNowTime())
                    .setRemark("过期时间是：" + DateUtil.format(DateUtil.endOfDay(DateUtil.parse(userBlackList.getExpireTime())),
                            "yyyy-MM-dd HH:mm:ss"))
                    .setReason("运维举报拉黑，运维端处理")
                    .setPhotosUrl(photosUrl)
                    .setAddAccountUserPhone(accountUser.getPhone())
                    .setAddAccountUserId(accountUser.getAccountUserId());
            blackListLogService.insertBatch(Collections.singletonList(blackListLog));
            violationLog.setHandleDesc("拉黑用户，到期时间：" + blockTime);
            this.handle(violationLog);
        } else {
            this.handle(violationLog);
        }
    }
}
